package com.joker.cart.controller;


import com.joker.cart.domain.CartVO;
import com.joker.cart.service.IPmsCartService;
import com.joker.cart.service.IPmsCartSkuService;
import com.joker.common.core.domain.Result;
import com.joker.common.core.web.controller.BaseController;

import com.joker.common.security.utils.SecurityUtils;
import com.joker.system.common.model.LoginUser;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 购物车Controller
 *
 * @author jorker
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/")
public class PmsCartController extends BaseController
{

    @Autowired
    private IPmsCartService pmsCartService;
    @Autowired
    private IPmsCartSkuService pmsCartSkuService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取选中的商品
     */
    @GetMapping("/listCheck")
    public Result listCheck() {
        List<CartVO> cartVOList = pmsCartService.listCheck();
        return Result.success(cartVOList);
    }
    /**
     * 获取登录人购物车列表
     */
    @GetMapping("/list")
    public Result list() {
        List<CartVO> cartVOList = pmsCartService.showCart();
        return Result.success(cartVOList);
    }

    @PutMapping("/updateSkuCount/{skuId}/{count}")
    public Result updateSkuCount(@PathVariable Long skuId, @PathVariable Long count){
        pmsCartService.updateSkuCount(skuId,count);
        return Result.success();
    }
    @PutMapping("/updateCheck/{skuId}/{checked}")
    public Result updateCheck(@PathVariable Long skuId, @PathVariable Boolean checked){
        pmsCartService.updateCheck(skuId,checked);
        return Result.success();
    }


    @DeleteMapping("/removePayed")
    public Result removePayed(@RequestParam("skuIds") Long[] skuIds){
        pmsCartService.removePayed(skuIds);
        return Result.success();
    }

    @DeleteMapping("/removeSkuFromCart/{skuId}")
    public Result removeSkuFromCart(@PathVariable Long skuId){
        pmsCartService.removeSkuFromCart(skuId);
        return Result.success();
    }


    @PostMapping("/addSkuInCart/{skuId}/{count}")
    public Result addSkuInCart(@PathVariable Long skuId, @PathVariable Long count){
        pmsCartService.addSkuInCart(skuId,count);
        return Result.success();
    }

//    @GetMapping("/getCartSku/{skuId}")
//    public Result getCartSku(@PathVariable Long skuId){
//
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        Long userid = loginUser.getUserid();
//
//        String script = "if tonumber(redis.call('get',KEYS[1]))>0 then redis.call('decr',KEYS[1]) redis.call('lpush',KEYS[2],ARGV[1]) return 1 else return 0 end";
//        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
//        redisScript.setScriptText(script);
//        redisScript.setResultType(Long.class);
//
//        ArrayList<Object> keyList = new ArrayList<>();
//        keyList.add("goods:"+skuId);
//        keyList.add("user:"+userid);
//
//        Long result = (Long) redisTemplate.execute(redisScript, keyList, String.valueOf(userid));
//
//        ValueOperations<String,Object> ops = redisTemplate.opsForValue();
//        ListOperations listOperations = redisTemplate.opsForList();
//
//        if (result == 1){
//            System.out.println(ops.get("goods:"+skuId));
//            System.out.println(listOperations.range("user:"+userid,0,-1));
//            return Result.success();
//        }else {
//            return Result.error();
//        }
//    }

}
